package com.ruoyi.webs.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;


public class MoneyUtils {
    public static DecimalFormat fnum = new DecimalFormat("##0.00");

    private static final String[] CN_UPPER_NUMBER = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] RADICES = new String[]{"", "拾", "佰", "仟"};
    private static final String[] BIG_RADICES = new String[]{"", "万", "亿", "兆"};


    public static String formatMoney(String value) {
        if (value == null || value == "") {
            value = "0.00";
        }
        return fnum.format(new BigDecimal(value));
    }


    public static String moneyAdd(String valueStr, String addStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal augend = new BigDecimal(addStr);
        fnum.setRoundingMode(RoundingMode.HALF_UP);
        return fnum.format(value.add(augend));
    }


    public static BigDecimal moneyAdd(BigDecimal value, BigDecimal augend) {
        return value.add(augend);
    }


    public static String moneySub(String valueStr, String minusStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal subtrahend = new BigDecimal(minusStr);
        return fnum.format(value.subtract(subtrahend));
    }


    public static BigDecimal moneySub(BigDecimal value, BigDecimal subtrahend) {
        return value.subtract(subtrahend);
    }


    public static String moneyMul(String valueStr, String mulStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal mulValue = new BigDecimal(mulStr);
        return fnum.format(value.multiply(mulValue));
    }


    public static BigDecimal moneyMul(BigDecimal value, BigDecimal mulValue) {
        return value.multiply(mulValue);
    }


    public static String moneydiv(String valueStr, String divideStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal divideValue = new BigDecimal(divideStr);
        return fnum.format(value.divide(divideValue, 2, 4));
    }


    public static BigDecimal moneydiv(BigDecimal value, BigDecimal divideValue) {
        return value.divide(divideValue, 2, 4);
    }


    public static boolean moneyComp(String valueStr, String compValueStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal compValue = new BigDecimal(compValueStr);

        int result = value.compareTo(compValue);
        if (result >= 0) {
            return true;
        }
        return false;
    }


    public static boolean moneyComp(BigDecimal valueStr, BigDecimal compValueStr) {
        int result = valueStr.compareTo(compValueStr);
        if (result >= 0) {
            return true;
        }
        return false;
    }


    public static String moneyMulOfNotPoint(String valueStr, String divideStr) {
        BigDecimal value = new BigDecimal(valueStr);
        BigDecimal mulValue = new BigDecimal(divideStr);
        valueStr = fnum.format(value.multiply(mulValue));
        return fnum.format(value.multiply(mulValue)).substring(0, valueStr.length() - 3);
    }


    public static String addComma(String str) {
        try {
            String banNum = "";
            if (str.contains(".")) {
                String[] arr = str.split("\\.");
                if (arr.length == 2) {
                    str = arr[0];
                    banNum = "." + arr[1];
                }
            }

            String reverseStr = (new StringBuilder(str)).reverse().toString();
            String strTemp = "";
            for (int i = 0; i < reverseStr.length(); i++) {
                if (i * 3 + 3 > reverseStr.length()) {
                    strTemp = String.valueOf(strTemp) + reverseStr.substring(i * 3, reverseStr.length());
                    break;
                }
                strTemp = String.valueOf(strTemp) + reverseStr.substring(i * 3, i * 3 + 3) + ",";
            }

            if (strTemp.endsWith(",")) {
                strTemp = strTemp.substring(0, strTemp.length() - 1);
            }

            String resultStr = (new StringBuilder(strTemp)).reverse().toString();
            resultStr = String.valueOf(resultStr) + banNum;
            return resultStr;
        } catch (Exception e) {
            return str;
        }
    }


    private static final String[] NUMBER = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] NUMBER2 = new String[]{"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] CBit = new String[]{"", "拾", "佰", "仟"};


    public static String capitalization(String szNum) {
        StringBuilder resstr = new StringBuilder();
        String tmpstr = szNum.trim();
        int sl = tmpstr.length();
        int sp = 0;
        int dotpos = tmpstr.indexOf('.');
        if (dotpos != -1) {
            while (sl > 1 && tmpstr.charAt(sl - 1) == '0')
                sl--;
            if (tmpstr.charAt(sl - 1) == '.')
                sl--;
            if (sl != tmpstr.length()) {
                tmpstr = tmpstr.substring(0, sl);
            }
        } else {
            dotpos = sl;
        }
        if (sl < 1)
            return NUMBER[0];
        if (tmpstr.charAt(0) == '-') {
            resstr.append("负");
            sp = 1;
        }
        String integerNum = tmpstr.substring(sp, dotpos - sp);
        String decimalNum = "";
        if (dotpos + 1 < sl)
            decimalNum = tmpstr.substring(dotpos + 1);
        sl = integerNum.length();
        sp = 0;
        while (sp < sl && integerNum.charAt(sp) == '0')
            sp++;
        if (sp > 0)
            integerNum = integerNum.substring(sp);
        int inl = integerNum.length();
        if (inl > 0) {
            int h = (inl - 1) % 4;
            int j = (inl - 1) / 4 + 1;
            sp = 0;
            boolean allzero = false;
            boolean preallzero = false;
            for (; j > 0; j--) {
                int k = h;
                h = 3;
                boolean preiszero = allzero;
                allzero = true;
                for (; k >= 0; k--, sp++) {
                    if (integerNum.charAt(sp) == '0') {
                        preiszero = true;
                    } else {
                        allzero = false;
                        if (preiszero)
                            resstr.append("零");
                        preiszero = false;
                        resstr.append(NUMBER[(byte) integerNum.charAt(sp) - 48]).append(CBit[k]);
                    }
                }
                if (j % 2 == 0) {
                    if (!allzero) {
                        resstr.append("万");
                    }
                } else if (!allzero || !preallzero) {
                    int repyi = j / 2;
                    for (int i = 0; i < repyi; i++) {
                        resstr.append("亿");
                    }
                }
                preallzero = allzero;
            }
        } else {
            resstr.append("零");
        }
        int dnl = decimalNum.length();
        if (dnl > 0) {
            resstr.append("点");
            for (int i = 0; i < dnl; i++) {
                resstr.append(NUMBER[(byte) decimalNum.charAt(i) - 48]);
            }
        }
        return resstr.toString();
    }


    public static char getNumByte(String szNum, int nBit) {
        int sl = szNum.length();
        int nPos = 0;
        while (nPos < sl && szNum.charAt(nPos) != '.')
            nPos++;
        if (nBit < 0) {
            nPos -= nBit;
        } else {
            nPos = nPos - nBit - 1;
        }
        if (nPos < 0 || nPos >= sl)
            return '0';
        return szNum.charAt(nPos);
    }


    public static String rmbChange(String rmb) {
        String zs = capitalization((rmb.indexOf('.') >= 0) ? rmb.substring(0, rmb.indexOf('.')) : rmb);
        if (String.valueOf(getNumByte(rmb, -2)).equals("0")) {
            if (String.valueOf(getNumByte(rmb, -1)).equals("0")) {
                return String.valueOf(zs) + "元整";
            }
            return String.valueOf(zs) + "元" +
                    capitalization(String.valueOf(getNumByte(rmb, -1))) + "角整";
        }

        if (String.valueOf(getNumByte(rmb, -1)).equals("0")) {
            return String.valueOf(zs) + "元" +
                    capitalization(String.valueOf(getNumByte(rmb, -1))) +
                    capitalization(String.valueOf(getNumByte(rmb, -2))) + "分";
        }
        return String.valueOf(zs) + "元" +
                capitalization(String.valueOf(getNumByte(rmb, -1))) + "角" +
                capitalization(String.valueOf(getNumByte(rmb, -2))) + "分";
    }


    public static String changeCN(String szNum) {
        StringBuilder sb = new StringBuilder();
        String str = szNum.trim();
        int sl = str.length();
        int sp = 0;

        if (sl < 1)
            return NUMBER2[0];
        for (; sp < sl; sp++) {
            if (str.charAt(sp) >= '0' && str.charAt(sp) <= '9') {
                sb.append(NUMBER2[str.charAt(sp) - 48]);
            } else {
                sb.append(str.charAt(sp));
            }
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        Double a = Double.valueOf(2332.965D);
        Double b = Double.valueOf(0.0D);

        String rs = moneyAdd(a.toString(), b.toString());
        System.out.println(rs);
        System.out.println(fnum.getRoundingMode());
    }
}